package znbkDialog;

import android.app.Dialog;
import android.content.Context;
import android.os.Bundle;
import android.text.Html;
import android.view.LayoutInflater;
import android.view.View;
import android.view.Window;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.example.lancooznbk.R;
import com.example.lancooznbk.presenter.PaperDef;

import lancooControl.Control;
import znbkTestPaperInfor.ZnbkPaperDef;

import static znbkTestPaperRdqh.activity.WeekAscensionPaperActivity.mEntityRdqh;
import static znbkTestPaperXtfx.activity.SystemReviewPaperActivity.mEntityXtfx;

/**
 * Created by Administrator on 2018\3\26 0026.
 */

public class ZnbkDialogNoAssessSubmit extends Dialog {

    private Context mContext;
    private int PaperFrom;
    private ClickListenerInterface clickListenerInterface;
    public static Button mBtnGoAnswer;
    public static Button mBtnSubmit;
    public static TextView mTvDialogTitle;
    public static TextView mTvDialogDetails;
    public static LinearLayout LayoutButton;



    public interface ClickListenerInterface {
        public void doGoAnswer();
        public void doSubmit();
    }

    public ZnbkDialogNoAssessSubmit(Context context, int paperFrom) {
        super(context, R.style.CustomDialog);
        this.mContext = context;
        this.PaperFrom = paperFrom;
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_NO_TITLE);

        init();

        getWindow().setBackgroundDrawableResource(R.color.vifrification);
    }

    public void init() {
        LayoutInflater inflater = LayoutInflater.from(mContext);
        View view = inflater.inflate(R.layout.znbk_dialog_no_assess_submit, null);
        setContentView(view);

        mTvDialogTitle = (TextView) findViewById(R.id.tv_dialogTitle);
        mTvDialogDetails = (TextView) findViewById(R.id.tv_dialogDetails);

        mBtnSubmit = (Button) findViewById(R.id.btn_submit);
        mBtnSubmit.setOnClickListener(new ZnbkDialogNoAssessSubmit.clickListener());

        mBtnGoAnswer = (Button) findViewById(R.id.btn_goAnswer);
        mBtnGoAnswer.setOnClickListener(new ZnbkDialogNoAssessSubmit.clickListener());

        LayoutButton = (LinearLayout) findViewById(R.id.layout_button);

        setCanceledOnTouchOutside(false);

//        int answeredItemCount = 0;
//        int totalItemCount = 0;
//        if(PaperFrom == PaperDef.FROM_BASE_REVIEW) {
//            for (int i = 0; i < InforBasicReview.getPaperContent().size(); i++) {
//                answeredItemCount = answeredItemCount + InforBasicReview.getPaperContent().get(i).getAnsweredItemCount();
//                totalItemCount = totalItemCount + InforBasicReview.getPaperContent().get(i).getTotalItemCount();
//            }
//
//        }else if(PaperFrom == PaperDef.FROM_DIFFERENT_REVIEW){
//
//            answeredItemCount = answeredItemCount + InforDifficultReview.getAnsweredItemCount();
//            totalItemCount = totalItemCount + InforDifficultReview.getTotalItemCount();
//
//        }else if(PaperFrom == PaperDef.FROM_GUESS_REVIEW){
//            for (int i = 0; i < InforGuessTesting.getPaperContent().size(); i++) {
//                answeredItemCount = answeredItemCount + InforGuessTesting.getPaperContent().get(i).getAnsweredItemCount();
//                totalItemCount = totalItemCount + InforGuessTesting.getPaperContent().get(i).getTotalItemCount();
//            }
//        }else if(PaperFrom == PaperDef.FROM_BASE_REVIEW_WRONG||PaperFrom == PaperDef.FROM_DIFFERENT_REVIEW_WRONG){
//
//            for (int i = 2; i < InforWrongQues.size(); i++) {
//                answeredItemCount = answeredItemCount + InforWrongQues.get(i).getAnsweredItemCount();
//                totalItemCount = totalItemCount + InforWrongQues.get(i).getTotalItemCount();
//            }
//
//        }
//        int neverAnsweredCount = totalItemCount-answeredItemCount;
//        setTitle(neverAnsweredCount);



        int NoAnsweredItemCount = -1;

        if(PaperFrom==ZnbkPaperDef.FROM_SYSTEM_REVIEW){

            int sumItemAnswerDetailList = mEntityXtfx.getModelSinglePaperInfo().getModelPaperInfo().getTotalItemCount();
            int BigAnsweredItemCount = 0;

            for(int i=0;i<sumItemAnswerDetailList;i++) {
                int TotalItemCount = mEntityXtfx.getModelSinglePaperInfo().getModelTKQueList().get(i).getTotalItemCount();
                int ChildIntemCount = mEntityXtfx.getModelSinglePaperInfo().getModelTKQueList().get(i).getChildList().size();
                int AnsweredItemCount = 0;
                if (TotalItemCount == ChildIntemCount) {
                    for (int m = 0; m < TotalItemCount; m++) {
                        String UpLoadPath = mEntityXtfx.getModelSinglePaperInfo().getModelTKQueList().get(i).
                                getChildList().get(m).getSubChildList().get(0).getUpLoadPath();
                        String Answer = mEntityXtfx.getModelSinglePaperInfo().getModelTKQueList().get(i).
                                getChildList().get(m).getSubChildList().get(0).getSubChildAnswer();
                        String StuAnswer = mEntityXtfx.getModelSinglePaperInfo().getModelTKQueList().get(i).
                                getChildList().get(m).getSubChildList().get(0).getSubChildStuAnswer();
                        String StuAssess = mEntityXtfx.getModelSinglePaperInfo().getModelTKQueList().get(i).
                                getChildList().get(m).getSubChildList().get(0).getSubChildStuAssess();

                        if (!StuAnswer.equals("") || !StuAssess.equals("") || !UpLoadPath.equals("")) {
                            AnsweredItemCount++;
                        }


                    }
                } else if (TotalItemCount > ChildIntemCount) {
                    String UpLoadPath = mEntityXtfx.getModelSinglePaperInfo().getModelTKQueList().get(i).
                            getChildList().get(0).getSubChildList().get(0).getUpLoadPath();
                    String Answer = mEntityXtfx.getModelSinglePaperInfo().getModelTKQueList().get(i).
                            getChildList().get(0).getSubChildList().get(0).getSubChildAnswer();
                    String StuAnswer = mEntityXtfx.getModelSinglePaperInfo().getModelTKQueList().get(i).
                            getChildList().get(0).getSubChildList().get(0).getSubChildStuAnswer();
                    String StuAssess = mEntityXtfx.getModelSinglePaperInfo().getModelTKQueList().get(i).
                            getChildList().get(0).getSubChildList().get(0).getSubChildStuAssess();

                    if (StuAssess.equals("")&UpLoadPath.equals("")) {

                        if (!StuAnswer.equals("")) {
                            for (int p = 0; p < TotalItemCount; p++) {
                                if (!Control.returnArray(StuAnswer, "$、", 2)[p].equals("")) {
                                    AnsweredItemCount++;
                                }
                            }
                        }
                    } else {
                        AnsweredItemCount = TotalItemCount;
                    }


                }

                if (AnsweredItemCount==TotalItemCount) {
                    BigAnsweredItemCount++;
                }

            }
            NoAnsweredItemCount = sumItemAnswerDetailList-BigAnsweredItemCount;
        }else if(PaperFrom==ZnbkPaperDef.FROM_WEEK_ASCENSION){
            int sumItemAnswerDetailList = mEntityRdqh.getModelSinglePaperInfo().getModelPaperInfo().getTotalItemCount();
            int BigAnsweredItemCount = 0;

            for(int i=0;i<sumItemAnswerDetailList;i++) {
                int TotalItemCount = mEntityRdqh.getModelSinglePaperInfo().getModelTKQueList().get(i).getTotalItemCount();
                int ChildIntemCount = mEntityRdqh.getModelSinglePaperInfo().getModelTKQueList().get(i).getChildList().size();
                int AnsweredItemCount = 0;
                if (TotalItemCount == ChildIntemCount) {
                    for (int m = 0; m < TotalItemCount; m++) {
                        String UpLoadPath = mEntityRdqh.getModelSinglePaperInfo().getModelTKQueList().get(i).
                                getChildList().get(m).getSubChildList().get(0).getUpLoadPath();
                        String Answer = mEntityRdqh.getModelSinglePaperInfo().getModelTKQueList().get(i).
                                getChildList().get(m).getSubChildList().get(0).getSubChildAnswer();
                        String StuAnswer = mEntityRdqh.getModelSinglePaperInfo().getModelTKQueList().get(i).
                                getChildList().get(m).getSubChildList().get(0).getSubChildStuAnswer();
                        String StuAssess = mEntityRdqh.getModelSinglePaperInfo().getModelTKQueList().get(i).
                                getChildList().get(m).getSubChildList().get(0).getSubChildStuAssess();

                        if (!StuAnswer.equals("") || !StuAssess.equals("") || !UpLoadPath.equals("")) {
                            AnsweredItemCount++;
                        }


                    }
                } else if (TotalItemCount > ChildIntemCount) {
                    String UpLoadPath = mEntityRdqh.getModelSinglePaperInfo().getModelTKQueList().get(i).
                            getChildList().get(0).getSubChildList().get(0).getUpLoadPath();
                    String Answer = mEntityRdqh.getModelSinglePaperInfo().getModelTKQueList().get(i).
                            getChildList().get(0).getSubChildList().get(0).getSubChildAnswer();
                    String StuAnswer = mEntityRdqh.getModelSinglePaperInfo().getModelTKQueList().get(i).
                            getChildList().get(0).getSubChildList().get(0).getSubChildStuAnswer();
                    String StuAssess = mEntityRdqh.getModelSinglePaperInfo().getModelTKQueList().get(i).
                            getChildList().get(0).getSubChildList().get(0).getSubChildStuAssess();

                    if (StuAssess.equals("")&UpLoadPath.equals("")) {

                        if (!StuAnswer.equals("")) {
                            for (int p = 0; p < TotalItemCount; p++) {
                                if (!Control.returnArray(StuAnswer, "$、", 2)[p].equals("")) {
                                    AnsweredItemCount++;
                                }
                            }
                        }
                    } else {
                        AnsweredItemCount = TotalItemCount;
                    }


                }

                if (AnsweredItemCount==TotalItemCount) {
                    BigAnsweredItemCount++;
                }

            }
            NoAnsweredItemCount = sumItemAnswerDetailList-BigAnsweredItemCount;
        }

        if(PaperFrom==ZnbkPaperDef.FROM_SYSTEM_REVIEW||
                PaperFrom==ZnbkPaperDef.FROM_WEEK_ASCENSION) {
            mTvDialogDetails.setVisibility(View.VISIBLE);
            if(NoAnsweredItemCount!=0) {
                mTvDialogDetails.setText("还有" + NoAnsweredItemCount + "道题未做完");
            }else{
                mTvDialogDetails.setText("已经全部作答完毕");
            }

        }else{
            mTvDialogDetails.setVisibility(View.GONE);
        }

    }

//    public void setTitle(int neverAnswered){
//        if(neverAnswered==0){
//            mTvDialogTitle.setText(Html.fromHtml("已经全部做完，是否提交？"));
//        }else{
//            mTvDialogTitle.setText(Html.fromHtml("还有" + "<font color='#ff323d'><middle><middle>" + neverAnswered + "</middle></middle></font>"
//                    + "道小题没有做完，确定提交？"));
//        }
//    }

    public void setClicklistener( ClickListenerInterface clickListenerInterface) {
        this.clickListenerInterface = clickListenerInterface;
    }


    private class clickListener implements View.OnClickListener {
        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub
            int id = v.getId();
            switch (id) {

                case R.id.btn_goAnswer:
                    clickListenerInterface.doGoAnswer();
                    break;

                case R.id.btn_submit:
                    clickListenerInterface.doSubmit();
                    break;

                default:
                    System.out.println("-----ooooooo11111defalt----------------");
                    break;
            }
        }

    };

}